﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data;
using CMISLibrary.Libraries;
using CMISLibrary.Entities;
using BaocaoNPC.Usercontrols.reports;
using BaocaoNPC.common;
using CMISLibrary;
using System.Threading;
using bus_TienichNPC;

//using NPCUS_DanhMuc;

namespace CMIS2.BaocaoNPC.Usercontrols
{
    /// <summary>
    /// Interaction logic for NPC_BCKD.xaml
    /// </summary>
    public partial class NPC_BCKD : UserControl
    {
        #region KhaiBaoBien
        /*
        public String strMa_dviqly = UserInfomation.MaDViqly != "" && UserInfomation.MaDViqly != null ? UserInfomation.MaDViqly : "PA";
        public String strTenDNHap = UserInfomation.TenDNHap != "" && UserInfomation.TenDNHap != null ? UserInfomation.TenDNHap : "PA";
        public String strCurrentLibID = MenuInfomation.LibID != "" && MenuInfomation.LibID != null ? MenuInfomation.LibID : "65";
        public String strTen_dviqly = UserInfomation.TenDViQLy != "" && UserInfomation.TenDViQLy != null ? UserInfomation.TenDViQLy : "cty..";
        public String strTen_dviqly_CT = UserInfomation.TenDViQLy_CT != "" && UserInfomation.TenDViQLy_CT != null ? UserInfomation.TenDViQLy_CT : "Cong ty Dien luc...";
         * */
        public String strMa_dviqly = NPCUS_DanhMuc.Pglobal.strMa_dviqly != "" && NPCUS_DanhMuc.Pglobal.strMa_dviqly != null ? NPCUS_DanhMuc.Pglobal.strMa_dviqly : "PA";
        public String strTenDNHap = NPCUS_DanhMuc.Pglobal.strTen_dangnhap != "" && NPCUS_DanhMuc.Pglobal.strTen_dangnhap != null ? NPCUS_DanhMuc.Pglobal.strTen_dangnhap : "PA";
        public String strTen_dviqly = NPCUS_DanhMuc.Pglobal.strTen_dviqly != "" && NPCUS_DanhMuc.Pglobal.strTen_dviqly != null ? NPCUS_DanhMuc.Pglobal.strTen_dviqly : "cty..";
        public String strTen_dviqly_CT = NPCUS_DanhMuc.Pglobal.strTen_dviqly_CT != "" && NPCUS_DanhMuc.Pglobal.strTen_dviqly_CT != null ? NPCUS_DanhMuc.Pglobal.strTen_dviqly_CT : "cap tren";

        public Int16 i16ThangBC = 0;
        public Int16 i16NamBC = 0;

        public Int16 i16ThangInBC = 0;
        public Int16 i16NamInBC = 0;
        public string strMa_dviqly_InBC = "";
        public string strTen_dviqly_InBC = "";
        public string strTen_dviqly_CT_InBC = "";

        int intFormLoad = 0;
        public DataSet ds_BCKD_In = new DataSet();
        public DataSet ds_BCKD_QTTH = new DataSet();
        public String strInBieuBCKD = "";
        public String p_nhapthop = "TH";
        public String p_dncd = "N";
        public String p_quy = "N";
        public bool checkBiaBC = false;
        DataSet ds_PC1_BIABC = new BCNPCEntities.CMIS2.PC1_BIABC();

        #endregion

        public NPC_BCKD()
        {
            try
            {
                InitializeComponent();
                GET_THANGNAM();


                btnInBCKD.IsEnabled = true;
                btnTongHop.IsEnabled = false;
                btnLaymau.Visibility = Visibility.Hidden;
                btnGhimau.Visibility = Visibility.Hidden;
                btnGhimau.IsEnabled = false;
                btnLaymau.IsEnabled = false;
                dtpThangNam_1.IsEnabled = false;
                chkLuyKe.IsEnabled = false;

                txtThangBcao.Text = i16ThangBC.ToString();
                txtNamBcao.Text = i16NamBC.ToString();

                intFormLoad = 1;
                if (i16ThangBC.ToString().Length == 1)
                {
                    dtpThangNam.Text = "0" + i16ThangBC.ToString() + "/" + i16NamBC.ToString();
                    dtpThangNam_1.Text = "0" + i16ThangBC.ToString() + "/" + i16NamBC.ToString();
                }
                else
                {
                    dtpThangNam.Text = i16ThangBC.ToString() + "/" + i16NamBC.ToString();
                    dtpThangNam_1.Text = i16ThangBC.ToString() + "/" + i16NamBC.ToString();
                }

                GetD_DVI_QLY();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
        #region Function_process_Data()

        private void GET_THANGNAM_MADVIQLY_INBC()
        {
            try
            {
                i16ThangInBC = Convert.ToInt16(dtpThangNam.Text.Substring(0, 2));
                i16NamInBC = Convert.ToInt16(dtpThangNam.Text.Substring(3, 4));
                strMa_dviqly_InBC = ((DataRowView)cboDonViQuanLy.SelectedItem)["MA_DVIQLY"].ToString();
                strTen_dviqly_InBC = ((DataRowView)cboDonViQuanLy.SelectedItem)["TEN_DVIQLY"].ToString();
                strTen_dviqly_CT_InBC = strTen_dviqly_CT;

            }
            catch
            {
                i16ThangInBC = Convert.ToInt16(DateTime.Now.Month);
                i16NamInBC = Convert.ToInt16(DateTime.Now.Year);
                strMa_dviqly_InBC = strMa_dviqly;
                strTen_dviqly_InBC = strTen_dviqly;
                strTen_dviqly_CT_InBC = strTen_dviqly_CT;
            }
        }

        private void GET_THANGNAM()
        {
            try
            {
                DataSet dsthanglviec = new DataSet();
                //bus_TienichNPC.cls_TienichNPC_bus bus = new cls_TienichNPC_bus();
                //dsthanglviec = bus.get_thang_lviec();
                dsthanglviec = GlobalServices.Service_NPCUS.get_thang_lviec();
                if (dsthanglviec != null && dsthanglviec.Tables.Count > 0)
                {
                    foreach( DataRow dr in dsthanglviec.Tables[0].Rows )
                        if (dr["PHAN_HE"].ToString() == "BC" && dr["MA_DVIQLY"].ToString() == strMa_dviqly)
                        {
                            i16ThangBC = Convert.ToInt16(dr["THANG"]);
                            i16NamBC = Convert.ToInt16(dr["NAM"]);
                        }
                }

            }
            catch
            {
                i16ThangBC = Convert.ToInt16(DateTime.Now.Month);
                i16NamBC = Convert.ToInt16(DateTime.Now.Year);
            }
        }

        //private bool LoadData_NPC_BCKD(String strTendviqly, String strTendviqlyCT, String strMadviqly, Int16 iNam, Int16 iThang)
        //{
        //    try
        //    {
        //        //PC1Business.cls_PC1_BCKD_In cls = new PC1Business.cls_PC1_BCKD_In();
        //        if (optBCKD09.IsChecked == true)
        //        {
        //            ds_BCKD_In = GlobalServices.Service_NPCUS.select_BCKD11(strTendviqly, strTendviqlyCT, strMadviqly, iNam, iThang);
        //            strInBieuBCKD = "KDDN-9";
        //        }

        //        if (ds_BCKD_In == null)
        //        {
        //            System.Windows.MessageBox.Show("Dữ liệu biểu " + strInBieuBCKD + " không có. Bạn hãy tổng hợp lại số liệu.");
        //            return false;
        //        }
        //        else
        //        {
        //            return true;
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        System.Windows.MessageBox.Show(ex.Message.ToString());
        //        return false;
        //    }
        //}

        private void GetD_DVI_QLY()
        {
            try
            {
                DataSet dsD_DVI_QLY = new DataSet();

                //cls_TienichNPC_bus cls = new cls_TienichNPC_bus();
                
                //dsD_DVI_QLY = cls.select_D_DVI_QLY_ALL(strMa_dviqly);

                dsD_DVI_QLY = GlobalServices.Service_NPCUS.select_D_DVI_QLY_ALL(strMa_dviqly);

                if (dsD_DVI_QLY == null)
                {
                    System.Windows.MessageBox.Show("Không lấy được danh mục đơn vị quản lý.");
                }
                else
                {
                    DataView dv = dsD_DVI_QLY.Tables["D_DVI_QLY"].DefaultView;
                    cboDonViQuanLy.ItemsSource = dv;
                    cboDonViQuanLy.DisplayMemberPath = "TEN_DVIQLY";
                    cboDonViQuanLy.SelectedValuePath = "MA_DVIQLY";
                    cboDonViQuanLy.SelectedIndex = 0;

                    cboDonViQuanLy1.ItemsSource = dv;
                    cboDonViQuanLy1.DisplayMemberPath = "TEN_DVIQLY";
                    cboDonViQuanLy1.SelectedValuePath = "MA_DVIQLY";
                    cboDonViQuanLy1.SelectedIndex = 0;
                }
            }
            catch (Exception ex)
            {
                System.Windows.MessageBox.Show(ex.Message.ToString());
            }
        }
        #endregion



        private void btnIn_BCKD_Click(object sender, RoutedEventArgs e)
        {
            this.Cursor = Cursors.Wait;
            Application.Current.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Background, new ThreadStart(delegate { }));
            try
            {
                GET_THANGNAM_MADVIQLY_INBC();

                #region =============     BIA BAO CAO     ===================
                if (Trangbia.IsSelected == true)
                {
                    string strSobc = txtSobcao.Text.Trim().ToString();
                    string strNoidung = txtNoiDung.Text.Trim().ToString();
                    string strThang = txtThangBcao.Text.Trim().ToString();
                    string strNam = txtNamBcao.Text.Trim().ToString();

                    DataRow dr = ds_PC1_BIABC.Tables["PC1_BIABC"].NewRow();

                    dr["TEN_DVIQLY"] = strTen_dviqly;
                    dr["TEN_CAPTREN"] = strTen_dviqly_CT;
                    dr["MA_DVIQLY"] = strMa_dviqly_InBC;
                    dr["NOI_DUNG"] = strNoidung;
                    dr["SO_BCAO"] = strSobc;
                    dr["THANG"] =strThang;
                    dr["NAM"] = strNam;

                    ds_PC1_BIABC.Tables["PC1_BIABC"].Rows.Add(dr);
                    ds_PC1_BIABC.Tables["PC1_BIABC"].AcceptChanges();

                    strInBieuBCKD = "BIA";

                    rpt_PC1_BIA_BC crt = new rpt_PC1_BIA_BC();
                    rpt_PC1_BIA_BC2 crt2 = new rpt_PC1_BIA_BC2();
                    
                    if (ds_PC1_BIABC != null)
                    {
                        if (ds_PC1_BIABC.Tables.Count > 0)
                        {
                            if (strMa_dviqly_InBC.Length == 6)
                            {
                                ds_PC1_BIABC.Tables[0].TableName = "PC1_BIABC";
                                crt2.SetDataSource(ds_PC1_BIABC.Tables["PC1_BIABC"]);
                                UtilReport.RptManager.showReportToWin(this, crt2, strMa_dviqly_InBC, new string[] { "Noinhan", "Trinhky" }, new string[] { "Noinhan", "Trinhky" });
                            }
                            else
                            {
                                ds_PC1_BIABC.Tables[0].TableName = "PC1_BIABC";
                                crt.SetDataSource(ds_PC1_BIABC.Tables["PC1_BIABC"]);
                                UtilReport.RptManager.showReportToWin(this, crt, strMa_dviqly_InBC, new string[] { "Noinhan", "Trinhky" }, new string[] { "Noinhan", "Trinhky" });
                            }
                        }
                        else
                        {
                            System.Windows.MessageBox.Show("Dữ liệu biểu " + strInBieuBCKD + " không có. Bạn hãy tổng hợp lại số liệu.");
                            return;
                        }
                    }
                    else
                    {
                        System.Windows.MessageBox.Show("Dữ liệu biểu " + strInBieuBCKD + " không có. Bạn hãy tổng hợp lại số liệu.");
                        return;
                    }
                    this.Cursor = null;
                    return;
                }
                #endregion

                #region =============     BIEU 00     =======================
                if (optBCKD00.IsChecked == true)
                {
                    ds_BCKD_In = GlobalServices.Service_NPCUS.select_PC1_BCKD00(strMa_dviqly_InBC, i16ThangInBC, i16NamInBC); //(strTendviqly, strTendviqlyCT, strMadviqly, iNam, iThang);
                    strInBieuBCKD = "KDDN-00";
                    rpt_PC1_BCKD00 crt = new rpt_PC1_BCKD00();
                    if (ds_BCKD_In != null)
                    {
                        if (ds_BCKD_In.Tables.Count > 0)
                        {
                            //ds_BCKD_In.Tables[0].TableName = "PC1_BCKD00";
                            crt.SetDataSource(ds_BCKD_In.Tables["PC1_BCKD00"]);
                            //UtilReport.RptManager.showReportToWin(this, crt, strMa_dviqly_InBC, new string[] { "Formula1", "Formula2", "Formula3" }, new string[] { "Formula1", "Formula2", "Formula3" });
                            UtilReportOMS.RptManager.showReportToWin(this, crt, strMa_dviqly_InBC, new string[] { "Formula1", "Formula2", "Formula3" }, new string[] { "Formula1", "Formula2", "Formula3" }, "CMIS2 - Report: Báo cáo NPC");
                        }
                        else
                        {
                            System.Windows.MessageBox.Show("Dữ liệu biểu " + strInBieuBCKD + " không có. Bạn hãy tổng hợp lại số liệu.");
                            return;
                        }
                    }
                    else
                    {
                        System.Windows.MessageBox.Show("Dữ liệu biểu " + strInBieuBCKD + " không có. Bạn hãy tổng hợp lại số liệu.");
                        return;
                    }
                }
                #endregion
                #region =============     BIEU 09     =======================
                if (optBCKD09.IsChecked == true)
                {
                    ds_BCKD_In = GlobalServices.Service_NPCUS.select_BCKD11(strTen_dviqly_InBC, strTen_dviqly_CT_InBC, strMa_dviqly_InBC, i16NamInBC, i16ThangInBC); //(strTendviqly, strTendviqlyCT, strMadviqly, iNam, iThang);
                    strInBieuBCKD = "KDDN-9";
                    rpt_PC1_BIEU_11 crt = new rpt_PC1_BIEU_11();
                    if (ds_BCKD_In != null)
                    {
                        if (ds_BCKD_In.Tables.Count > 0)
                        {
                            ds_BCKD_In.Tables[0].TableName = "PC1_BCKD11";
                            crt.SetDataSource(ds_BCKD_In.Tables["PC1_BCKD11"]);
                            //UtilReport.RptManager.showReportToWin(this, crt, strMa_dviqly_InBC, new string[] { "Formula1", "Formula2", "Formula3" }, new string[] { "Formula1", "Formula2", "Formula3" });
                            UtilReportOMS.RptManager.showReportToWin(this, crt, strMa_dviqly_InBC, new string[] { "Formula1", "Formula2", "Formula3" }, new string[] { "Formula1", "Formula2", "Formula3" }, "CMIS2 - Report: Báo cáo NPC");
                        }
                        else
                        {
                            System.Windows.MessageBox.Show("Dữ liệu biểu " + strInBieuBCKD + " không có. Bạn hãy tổng hợp lại số liệu.");
                            return;
                        }
                    }
                    else
                    {
                        System.Windows.MessageBox.Show("Dữ liệu biểu " + strInBieuBCKD + " không có. Bạn hãy tổng hợp lại số liệu.");
                        return;
                    }
                }
                #endregion

                #region=============     BIEU 10B     =======================
                if (optBCKD10_B.IsChecked == true)
                {
                    DataSet ds_bcttcc = new DataSet();
                    strInBieuBCKD = "KDDN-10B";
                    
                    
                    if (p_quy == "Y")
                    { ds_bcttcc = GlobalServices.Service_NPCUS.get_bctt_quy(strMa_dviqly_InBC, i16NamInBC, i16ThangInBC,101,"TH");
                    ds_bcttcc.Tables[0].TableName = "BCTTCC";
                    }
                    else
                    {
                        //bus_TienichNPC.cls_TienichNPC_bus bus = new cls_TienichNPC_bus();
                        //ds_bcttcc = bus.get_bcttcc(strMa_dviqly_InBC, i16ThangInBC, i16NamInBC);
                    ds_bcttcc = GlobalServices.Service_NPCUS.get_bcttcc(strMa_dviqly_InBC, i16ThangInBC, i16NamInBC);
                    }
                    //bus_TienichNPC.cls_TienichNPC_bus bus = new bus_TienichNPC.cls_TienichNPC_bus();
                    //ds_bcttcc = bus.get_bcttcc(strMa_dviqly_InBC, iThang, iNam);
                    rpt_NPC_BCTTCC rpt = new rpt_NPC_BCTTCC();
                    rpt_NPC_BCTTCC_2 rpt2 = new rpt_NPC_BCTTCC_2();
                    if (ds_bcttcc != null)
                    {
                        if (ds_bcttcc.Tables.Count > 0)
                        {
                            if (ds_bcttcc.Tables["BCTTCC"].Rows.Count > 0)
                            {
                                if (chk_groupXBT.IsChecked == true)
                                {
                                    rpt2.SetDataSource(ds_bcttcc.Tables["BCTTCC"]);
                                    rpt2.SetParameterValue("p_tendvi", strTen_dviqly_InBC); //ds3.Tables[0].Rows[0]["TEN_DVIQLY"].ToString());
                                    rpt2.SetParameterValue("p_tencaptren", strTen_dviqly_CT_InBC);//ds3.Tables[0].Rows[0]["TEN_DVIQLYCTREN"].ToString());
                                    //UtilReport.RptManager.showReportToWin(this, rpt2, strMa_dviqly_InBC, new string[] { "Formula1", "Formula2", "Formula3" }, new string[] { "Formula1", "Formula2", "Formula3" });
                                    UtilReportOMS.RptManager.showReportToWin(this, rpt2, strMa_dviqly_InBC, new string[] { "Formula1", "Formula2", "Formula3" }, new string[] { "Formula1", "Formula2", "Formula3" }, "CMIS2 - Report: Báo cáo NPC");
                                }
                                else
                                {
                                    rpt.SetDataSource(ds_bcttcc.Tables["BCTTCC"]);
                                    rpt.SetParameterValue("p_tendvi", strTen_dviqly_InBC); //ds3.Tables[0].Rows[0]["TEN_DVIQLY"].ToString());
                                    rpt.SetParameterValue("p_tencaptren", strTen_dviqly_CT_InBC);//ds3.Tables[0].Rows[0]["TEN_DVIQLYCTREN"].ToString());
                                    //UtilReport.RptManager.showReportToWin(this, rpt, strMa_dviqly_InBC, new string[] { "Formula1", "Formula2", "Formula3" }, new string[] { "Formula1", "Formula2", "Formula3" });
                                    UtilReportOMS.RptManager.showReportToWin(this, rpt, strMa_dviqly_InBC, new string[] { "Formula1", "Formula2", "Formula3" }, new string[] { "Formula1", "Formula2", "Formula3" }, "CMIS2 - Report: Báo cáo NPC");

                                }
                            }
                            else
                            {
                                System.Windows.MessageBox.Show("Dữ liệu biểu " + strInBieuBCKD + " không có. Bạn hãy tổng hợp lại số liệu.");
                                return;
                            }
                        }
                        else
                        {
                            System.Windows.MessageBox.Show("Dữ liệu biểu " + strInBieuBCKD + " không có. Bạn hãy tổng hợp lại số liệu.");
                            return;
                        }
                    }
                    else
                    {
                        System.Windows.MessageBox.Show("Dữ liệu biểu " + strInBieuBCKD + " không có. Bạn hãy tổng hợp lại số liệu.");
                        return;
                    }

                }
                #endregion

                # region =============     BIEU 10A     =======================
                if (optBCKD10_A.IsChecked == true)
                {
                    DataSet ds_bctttt = new DataSet();
                    strInBieuBCKD = "KDDN-10A";
                    //string strTenDvi = "";
                    //string strTencaptren = "";
                    string strMocDN = "";
                    if (p_nhapthop == "2")
                    { strMocDN = "Mốc đầu nguồn 2"; }


                    //foreach (DataRow dr in ds_dvi.Tables[0].Rows)
                    //{
                    //    if (dr["MA_DVIQLY"].ToString() == strMa_dviqly_InBC)
                    //    {
                    //        strTenDvi = dr["TEN_DVIQLY"].ToString();
                    //        strTencaptren = dr["TEN_DVIQLYCTREN"].ToString();
                    //    }
                    //}
                    if (p_quy == "Y")
                    {
                        //bus_TienichNPC.cls_TienichNPC_bus bus = new bus_TienichNPC.cls_TienichNPC_bus();
                        //ds_bctttt = bus.get_bctt_quy(strMa_dviqly_InBC, i16NamInBC, i16ThangInBC,102,p_nhapthop);
                        ds_bctttt = GlobalServices.Service_NPCUS.get_bctt_quy(strMa_dviqly_InBC, i16NamInBC, i16ThangInBC,102,p_nhapthop);
                        ds_bctttt.Tables[0].TableName = "BCTTTT";
                    }
                    else
                    {
                   ds_bctttt = GlobalServices.Service_NPCUS.get_bctttt(strMa_dviqly_InBC, i16ThangInBC, i16NamInBC, p_nhapthop);
                    }
                   //ds_bctttt.WriteXmlSchema("D:\\CMIS2.0\\TIENICH_NPC\\CMIS2_NPC20111207\\CMIS2_NPC\\NPC_BCTTTT.xsd");
                   //bus_TienichNPC.cls_TienichNPC_bus bus = new bus_TienichNPC.cls_TienichNPC_bus();
                   //ds_bctttt = bus.get_bctttt(strMa_dviqly_InBC, i16ThangInBC, i16NamInBC, p_nhapthop);
                    if (ds_bctttt != null)
                    {
                        if (ds_bctttt.Tables.Count > 0)
                        {
                            if (ds_bctttt.Tables["BCTTTT"].Rows.Count > 0)
                            {
                                rpt_NPC_BCTTTT rpt = new rpt_NPC_BCTTTT();
                                rpt.SetDataSource(ds_bctttt.Tables["BCTTTT"]);
                                rpt.SetParameterValue("MocDN", strMocDN);
                                rpt.SetParameterValue("p_tendvi", strTen_dviqly_InBC);
                                rpt.SetParameterValue("p_tencaptren", strTen_dviqly_CT_InBC);
                                //UtilReport.RptManager.showReportToWin(this, rpt, strMa_dviqly_InBC, new string[] { "Formula1", "Formula2", "Formula3" }, new string[] { "Formula1", "Formula2", "Formula3" });
                                UtilReportOMS.RptManager.showReportToWin(this, rpt, strMa_dviqly_InBC, new string[] { "Formula1", "Formula2", "Formula3" }, new string[] { "Formula1", "Formula2", "Formula3" }, "CMIS2 - Report: Báo cáo NPC");
                            }
                            else
                            {
                                System.Windows.MessageBox.Show("Dữ liệu biểu " + strInBieuBCKD + " không có. Bạn hãy tổng hợp lại số liệu.");
                                return;
                            }
                        }
                        else
                        {
                            System.Windows.MessageBox.Show("Dữ liệu biểu " + strInBieuBCKD + " không có. Bạn hãy tổng hợp lại số liệu.");
                            return;
                        }
                    }
                    else
                    {
                        System.Windows.MessageBox.Show("Dữ liệu biểu " + strInBieuBCKD + " không có. Bạn hãy tổng hợp lại số liệu.");
                        return;
                    }
                }
                # endregion

                # region =============     BIEU 10C     =======================
                if (optBCKD10_C.IsChecked == true)
                {
                    DataSet ds_bcttdd = new DataSet();

                    //string strTenDvi = "";
                    //string strTencaptren = "";
                    strInBieuBCKD = "KDDN-10C";

                    //foreach (DataRow dr in ds_dvi.Tables[0].Rows)
                    //{
                    //    if (dr["MA_DVIQLY"].ToString() == strMa_dviqly_InBC)
                    //    {
                    //        strTenDvi = dr["TEN_DVIQLY"].ToString();
                    //        strTencaptren = dr["TEN_DVIQLYCTREN"].ToString();
                    //    }
                    //}
                    if (p_quy == "Y")
                    { ds_bcttdd = GlobalServices.Service_NPCUS.get_bctt_quy(strMa_dviqly_InBC, i16NamInBC, i16ThangInBC,103,"TH");
                    ds_bcttdd.Tables[0].TableName = "BCTTDD";
                    }

                    else
                    {
                    ds_bcttdd = GlobalServices.Service_NPCUS.get_bcttdd(strMa_dviqly_InBC, i16ThangInBC, i16NamInBC);
                    }
                    //bus_TienichNPC.cls_TienichNPC_bus bus = new bus_TienichNPC.cls_TienichNPC_bus();
                    //ds_bcttdd = bus.get_bcttdd(strMa_dviqly_InBC, i16ThangInBC, i16NamInBC);
                    //ds_bcttdd.WriteXmlSchema("D:\\Projects\\NPCUS\\NPC_BCTTDD.xsd");
                    if (ds_bcttdd != null)
                    {
                        if (ds_bcttdd.Tables.Count > 0)
                        {
                            if (ds_bcttdd.Tables["BCTTDD"].Rows.Count > 0)
                            {
                                rpt_NPC_BCTTDD rpt = new rpt_NPC_BCTTDD();
                                rpt.SetDataSource(ds_bcttdd.Tables["BCTTDD"]);
                                rpt.SetParameterValue("p_tendvi", strTen_dviqly_InBC); //ds3.Tables[0].Rows[0]["TEN_DVIQLY"].ToString());
                                rpt.SetParameterValue("p_tencaptren", strTen_dviqly_CT_InBC);//ds3.Tables[0].Rows[0]["TEN_DVIQLYCTREN"].ToString());
                                //UtilReport.RptManager.showReportToWin(this, rpt, strMa_dviqly_InBC, new string[] { "Formula1", "Formula2", "Formula3" }, new string[] { "Formula1", "Formula2", "Formula3" });
                                UtilReportOMS.RptManager.showReportToWin(this, rpt, strMa_dviqly_InBC, new string[] { "Formula1", "Formula2", "Formula3" }, new string[] { "Formula1", "Formula2", "Formula3" }, "CMIS2 - Report: Báo cáo NPC");
                            }
                            else
                            {
                                System.Windows.MessageBox.Show("Dữ liệu biểu " + strInBieuBCKD + " không có. Bạn hãy tổng hợp lại số liệu.");
                                return;
                            }
                        }
                        else
                        {
                            System.Windows.MessageBox.Show("Dữ liệu biểu " + strInBieuBCKD + " không có. Bạn hãy tổng hợp lại số liệu.");
                            return;
                        }
                    }
                    else
                    {
                        System.Windows.MessageBox.Show("Dữ liệu biểu " + strInBieuBCKD + " không có. Bạn hãy tổng hợp lại số liệu.");
                        return;
                    }

                }
                # endregion
                # region =============     BIEU 10D     =======================
                if (optBCKD10_D.IsChecked == true)
                {
                    DataSet ds_bctskt = new DataSet();
                    strInBieuBCKD = "KDDN-10D";
                    ds_bctskt = GlobalServices.Service_NPCUS.get_bctskt(strMa_dviqly_InBC, i16ThangInBC, i16NamInBC);   
                 
                    //bus_TienichNPC.cls_TienichNPC_bus bus = new bus_TienichNPC.cls_TienichNPC_bus();
                    //ds_bcttdd = bus.get_bcttdd(strMa_dviqly_InBC, i16ThangInBC, i16NamInBC);
                    //ds_bcttdd.WriteXmlSchema("D:\\Projects\\NPCUS\\NPC_BCTTDD.xsd");
                    if (ds_bctskt != null)
                    {
                        if (ds_bctskt.Tables.Count > 0)
                        {
                            if (ds_bctskt.Tables["BCTSKT"].Rows.Count > 0)
                            {
                                rpt_NPC_BCTSKT rpt = new rpt_NPC_BCTSKT();
                                rpt.SetDataSource(ds_bctskt.Tables["BCTSKT"]);
                                rpt.SetParameterValue("pTendienluc", strTen_dviqly);
                                rpt.SetParameterValue("pTencongty", strTen_dviqly_CT);
                                //UtilReport.RptManager.showReportToWin(this, rpt, strMa_dviqly, new string[] { "Formula1", "Formula2", "Formula3" }, new string[] { "Formula1", "Formula2", "Formula3" });
                                UtilReportOMS.RptManager.showReportToWin(this, rpt, strMa_dviqly_InBC, new string[] { "Formula1", "Formula2", "Formula3" }, new string[] { "Formula1", "Formula2", "Formula3" }, "CMIS2 - Report: Báo cáo NPC");
                            }
                            else
                            {
                                System.Windows.MessageBox.Show("Dữ liệu biểu " + strInBieuBCKD + " không có. Bạn hãy nhập số liệu.");
                                return;
                            }
                        }
                        else
                        {
                            System.Windows.MessageBox.Show("Dữ liệu biểu " + strInBieuBCKD + " không có. Bạn hãy tổng hợp lại số liệu.");
                            return;
                        }
                    }
                    else
                    {
                        System.Windows.MessageBox.Show("Dữ liệu biểu " + strInBieuBCKD + " không có. Bạn hãy tổng hợp lại số liệu.");
                        return;
                    }

                }
                # endregion

                # region =============     BIEU 11     =======================
                if (optBCKD11.IsChecked == true)
                {

                    strInBieuBCKD = "KDDN-11";


                    DataSet dscobo = GlobalServices.Service_NPCUS.INBIEU(strMa_dviqly_InBC, i16NamInBC, i16ThangInBC, 17);

                    if (dscobo != null)
                    {
                        if (dscobo.Tables.Count > 0)
                        {
                            rpt_PC1_BCKD17 rpt = new rpt_PC1_BCKD17();
                            rpt.SetDataSource(dscobo.Tables[0]);
                            //rpt.SetParameterValue("p_tendvi", strTen_dviqly_InBC); //ds3.Tables[0].Rows[0]["TEN_DVIQLY"].ToString());
                            //rpt.SetParameterValue("p_tencaptren", strTen_dviqly_CT_InBC);//ds3.Tables[0].Rows[0]["TEN_DVIQLYCTREN"].ToString());
                            //UtilReport.RptManager.showReportToWin(this, rpt, strMa_dviqly_InBC, new string[] { "Formula1", "Formula2", "Formula3" }, new string[] { "Formula1", "Formula2", "Formula3" });
                            UtilReportOMS.RptManager.showReportToWin(this, rpt, strMa_dviqly_InBC, new string[] { "Formula1", "Formula2", "Formula3" }, new string[] { "Formula1", "Formula2", "Formula3" }, "CMIS2 - Report: Báo cáo NPC");
                        }
                        else
                        {
                            System.Windows.MessageBox.Show("Dữ liệu biểu " + strInBieuBCKD + " không có. Bạn hãy tổng hợp lại số liệu.");
                            return;
                        }
                    }
                    else
                    {
                        System.Windows.MessageBox.Show("Dữ liệu biểu " + strInBieuBCKD + " không có. Bạn hãy tổng hợp lại số liệu.");
                        return;
                    }

                }
                # endregion

                # region =============     BIEU 12     =======================
                if (optBCKD12.IsChecked == true)
                {

                    strInBieuBCKD = "KDDN-12";


                    DataSet dscobo = GlobalServices.Service_NPCUS.INBIEU(strMa_dviqly_InBC, i16NamInBC, i16ThangInBC, 18);

                    if (dscobo != null)
                    {
                        if (dscobo.Tables.Count > 0)
                        {
                            rpt_PC1_BCKD18 rpt = new rpt_PC1_BCKD18();
                            rpt.SetDataSource(dscobo.Tables["PC1_BCKD18"]);
                            //rpt.SetParameterValue("p_tendvi", strTen_dviqly_InBC); //ds3.Tables[0].Rows[0]["TEN_DVIQLY"].ToString());
                            //rpt.SetParameterValue("p_tencaptren", strTen_dviqly_CT_InBC);//ds3.Tables[0].Rows[0]["TEN_DVIQLYCTREN"].ToString());
                            //UtilReport.RptManager.showReportToWin(this, rpt, strMa_dviqly_InBC, new string[] { "Formula1", "Formula2", "Formula3" }, new string[] { "Formula1", "Formula2", "Formula3" });
                            UtilReportOMS.RptManager.showReportToWin(this, rpt, strMa_dviqly_InBC, new string[] { "Formula1", "Formula2", "Formula3" }, new string[] { "Formula1", "Formula2", "Formula3" }, "CMIS2 - Report: Báo cáo NPC");
                        }
                        else
                        {
                            System.Windows.MessageBox.Show("Dữ liệu biểu " + strInBieuBCKD + " không có. Bạn hãy tổng hợp lại số liệu.");
                            return;
                        }
                    }
                    else
                    {
                        System.Windows.MessageBox.Show("Dữ liệu biểu " + strInBieuBCKD + " không có. Bạn hãy tổng hợp lại số liệu.");
                        return;
                    }

                }
                # endregion

                # region =============     BIEU 13     =======================
                if (optBCKD13.IsChecked == true)
                {

                    strInBieuBCKD = "KDDN-13";

                    //GlobalServices.Service_NPCUS.PC1_BIEU16_UP(strTen_dviqly, i16ThangInBC, i16NamInBC);
                    DataSet dscobo = GlobalServices.Service_NPCUS.select_BCKD16(strTen_dviqly_InBC, strTen_dviqly_CT_InBC, strMa_dviqly_InBC, i16ThangInBC, i16NamInBC); ;

                    if (dscobo != null)
                    {
                        if (dscobo.Tables.Count > 0)
                        {
                            rpt_PC1_BCKD16 rpt = new rpt_PC1_BCKD16();
                            rpt.SetDataSource(dscobo.Tables[0]);
                            //rpt.SetParameterValue("p_tendvi", strTen_dviqly_InBC); //ds3.Tables[0].Rows[0]["TEN_DVIQLY"].ToString());
                            //rpt.SetParameterValue("p_tencaptren", strTen_dviqly_CT_InBC);//ds3.Tables[0].Rows[0]["TEN_DVIQLYCTREN"].ToString());
                            //UtilReport.RptManager.showReportToWin(this, rpt, strMa_dviqly_InBC, new string[] { "Formula1", "Formula2", "Formula3" }, new string[] { "Formula1", "Formula2", "Formula3" });
                            UtilReportOMS.RptManager.showReportToWin(this, rpt, strMa_dviqly_InBC, new string[] { "Formula1", "Formula2", "Formula3" }, new string[] { "Formula1", "Formula2", "Formula3" }, "CMIS2 - Report: Báo cáo NPC");
                        }
                        else
                        {
                            System.Windows.MessageBox.Show("Dữ liệu biểu " + strInBieuBCKD + " không có. Bạn hãy tổng hợp lại số liệu.");
                            return;
                        }
                    }
                    else
                    {
                        System.Windows.MessageBox.Show("Dữ liệu biểu " + strInBieuBCKD + " không có. Bạn hãy tổng hợp lại số liệu.");
                        return;
                    }

                }
                # endregion

                # region =============     BIEU 4A     =======================
                if (optBCKD4A.IsChecked == true)
                {

                    strInBieuBCKD = "KDDN-4A";

                    //GlobalServices.Service_NPCUS.PC1_BIEU16_UP(strTen_dviqly, i16ThangInBC, i16NamInBC);
                    DataSet dscobo = GlobalServices.Service_NPCUS.select_BCKD4A(strTen_dviqly_InBC, strTen_dviqly_CT_InBC, strMa_dviqly_InBC, i16ThangInBC, i16NamInBC);

                    if (dscobo != null)
                    {
                        if (dscobo.Tables.Count > 0)
                        {
                            rpt_PC1_BCKD4A rpt = new rpt_PC1_BCKD4A();
                            rpt.SetDataSource(dscobo.Tables[0]);
                            //rpt.SetParameterValue("p_tendvi", strTen_dviqly_InBC); //ds3.Tables[0].Rows[0]["TEN_DVIQLY"].ToString());
                            //rpt.SetParameterValue("p_tencaptren", strTen_dviqly_CT_InBC);//ds3.Tables[0].Rows[0]["TEN_DVIQLYCTREN"].ToString());
                            //UtilReport.RptManager.showReportToWin(this, rpt, strMa_dviqly_InBC, new string[] { "Formula1", "Formula2", "Formula3" }, new string[] { "Formula1", "Formula2", "Formula3" });
                            UtilReportOMS.RptManager.showReportToWin(this, rpt, strMa_dviqly_InBC, new string[] { "Formula1", "Formula2", "Formula3" }, new string[] { "Formula1", "Formula2", "Formula3" }, "CMIS2 - Report: Báo cáo NPC");
                        }
                        else
                        {
                            System.Windows.MessageBox.Show("Dữ liệu biểu " + strInBieuBCKD + " không có. Bạn hãy tổng hợp lại số liệu.");
                            return;
                        }
                    }
                    else
                    {
                        System.Windows.MessageBox.Show("Dữ liệu biểu " + strInBieuBCKD + " không có. Bạn hãy tổng hợp lại số liệu.");
                        return;
                    }

                }
                # endregion

                # region =============     BIEU 4B     =======================
                if (optBCKD4B.IsChecked == true)
                {

                    strInBieuBCKD = "KDDN-4B";

                    //GlobalServices.Service_NPCUS.PC1_BIEU16_UP(strTen_dviqly, i16ThangInBC, i16NamInBC);
                    DataSet dscobo = GlobalServices.Service_NPCUS.select_BCKD4B(strTen_dviqly_InBC, strTen_dviqly_CT_InBC, strMa_dviqly_InBC, i16ThangInBC, i16NamInBC);

                    if (dscobo != null)
                    {
                        if (dscobo.Tables.Count > 0)
                        {
                            rpt_PC1_BCKD4B rpt = new rpt_PC1_BCKD4B();
                            rpt.SetDataSource(dscobo.Tables[0]);
                            //rpt.SetParameterValue("p_tendvi", strTen_dviqly_InBC); //ds3.Tables[0].Rows[0]["TEN_DVIQLY"].ToString());
                            //rpt.SetParameterValue("p_tencaptren", strTen_dviqly_CT_InBC);//ds3.Tables[0].Rows[0]["TEN_DVIQLYCTREN"].ToString());
                            //UtilReport.RptManager.showReportToWin(this, rpt, strMa_dviqly_InBC, new string[] { "Formula1", "Formula2", "Formula3" }, new string[] { "Formula1", "Formula2", "Formula3" });
                            UtilReportOMS.RptManager.showReportToWin(this, rpt, strMa_dviqly_InBC, new string[] { "Formula1", "Formula2", "Formula3" }, new string[] { "Formula1", "Formula2", "Formula3" }, "CMIS2 - Report: Báo cáo NPC");
                        }
                        else
                        {
                            System.Windows.MessageBox.Show("Dữ liệu biểu " + strInBieuBCKD + " không có. Bạn hãy tổng hợp lại số liệu.");
                            return;
                        }
                    }
                    else
                    {
                        System.Windows.MessageBox.Show("Dữ liệu biểu " + strInBieuBCKD + " không có. Bạn hãy tổng hợp lại số liệu.");
                        return;
                    }

                }
                # endregion

                # region =============     BIEU 5A     =======================
                if (optBCKD5A.IsChecked == true)
                {

                    strInBieuBCKD = "KDDN-5A";

                    //GlobalServices.Service_NPCUS.PC1_BIEU16_UP(strTen_dviqly, i16ThangInBC, i16NamInBC);
                    DataSet dscobo = GlobalServices.Service_NPCUS.select_BCKD5A(strTen_dviqly_InBC, strTen_dviqly_CT_InBC, strMa_dviqly_InBC, i16ThangInBC, i16NamInBC);

                    if (dscobo != null)
                    {
                        if (dscobo.Tables.Count > 0)
                        {
                            rpt_PC1_BCKD5A rpt = new rpt_PC1_BCKD5A();
                            rpt.SetDataSource(dscobo.Tables[0]);
                            //rpt.SetParameterValue("p_tendvi", strTen_dviqly_InBC); //ds3.Tables[0].Rows[0]["TEN_DVIQLY"].ToString());
                            //rpt.SetParameterValue("p_tencaptren", strTen_dviqly_CT_InBC);//ds3.Tables[0].Rows[0]["TEN_DVIQLYCTREN"].ToString());
                            //UtilReport.RptManager.showReportToWin(this, rpt, strMa_dviqly_InBC, new string[] { "Nguoilapbieu", "Trinhky" }, new string[] { "Nguoilapbieu", "Trinhky" });
                            UtilReportOMS.RptManager.showReportToWin(this, rpt, strMa_dviqly_InBC, new string[] { "Formula1", "Formula2" }, new string[] { "Formula1", "Formula2" }, "CMIS2 - Report: Báo cáo NPC");
                        }
                        else
                        {
                            System.Windows.MessageBox.Show("Dữ liệu biểu " + strInBieuBCKD + " không có. Bạn hãy tổng hợp lại số liệu.");
                            return;
                        }
                    }
                    else
                    {
                        System.Windows.MessageBox.Show("Dữ liệu biểu " + strInBieuBCKD + " không có. Bạn hãy tổng hợp lại số liệu.");
                        return;
                    }

                }
                # endregion

                # region =============     BIEU 5B     =======================
                if (optBCKD5B.IsChecked == true)
                {

                    strInBieuBCKD = "KDDN-5B";

                    //GlobalServices.Service_NPCUS.PC1_BIEU16_UP(strTen_dviqly, i16ThangInBC, i16NamInBC);
                    DataSet dscobo = GlobalServices.Service_NPCUS.select_BCKD5B(strTen_dviqly_InBC, strTen_dviqly_CT_InBC, strMa_dviqly_InBC, i16ThangInBC, i16NamInBC);

                    if (dscobo != null)
                    {
                        if (dscobo.Tables.Count > 0)
                        {
                            rpt_PC1_BCKD5B rpt = new rpt_PC1_BCKD5B();
                            rpt.SetDataSource(dscobo.Tables[0]);
                            //rpt.SetParameterValue("p_tendvi", strTen_dviqly_InBC); //ds3.Tables[0].Rows[0]["TEN_DVIQLY"].ToString());
                            //rpt.SetParameterValue("p_tencaptren", strTen_dviqly_CT_InBC);//ds3.Tables[0].Rows[0]["TEN_DVIQLYCTREN"].ToString());
                            //UtilReport.RptManager.showReportToWin(this, rpt, strMa_dviqly_InBC, new string[] { "Nguoilapbieu", "Trinhky" }, new string[] { "Nguoilapbieu", "Trinhky" });
                            UtilReportOMS.RptManager.showReportToWin(this, rpt, strMa_dviqly_InBC, new string[] { "Nguoilapbieu", "Trinhky" }, new string[] { "Nguoilapbieu", "Trinhky" }, "CMIS2 - Report: Báo cáo NPC");
                        }
                        else
                        {
                            System.Windows.MessageBox.Show("Dữ liệu biểu " + strInBieuBCKD + " không có. Bạn hãy tổng hợp lại số liệu.");
                            return;
                        }
                    }
                    else
                    {
                        System.Windows.MessageBox.Show("Dữ liệu biểu " + strInBieuBCKD + " không có. Bạn hãy tổng hợp lại số liệu.");
                        return;
                    }

                }
                # endregion

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                return;
            }

            this.Cursor = null;
        }

        private void btnTongHop_Click(object sender, RoutedEventArgs e)
        {
            this.Cursor = Cursors.Wait;
            Application.Current.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Background, new ThreadStart(delegate { }));
            try
            {
                GET_THANGNAM_MADVIQLY_INBC();

                //=============     BIEU KDDN-00     =======================
                if (optBCKD00.IsChecked == true)
                {
                    MessageBox.Show("Biểu 00 không cần tổng hợp");
                    return;
                }

                //=============     BIEU KDDN-09     =======================
                if (optBCKD09.IsChecked == true)
                {
                    //bus_TienichNPC.cls_TienichNPC_bus bus = new bus_TienichNPC.cls_TienichNPC_bus();
                    //ds_BCKD_QTTH = bus.TH_KDDN09(i16NamBC, i16ThangBC, strMa_dviqly);
                    ds_BCKD_QTTH = GlobalServices.Service_NPCUS.TH_KDDN09(i16NamBC, i16ThangBC, strMa_dviqly);
                    if (ds_BCKD_QTTH != null)
                    {
                        DataTable dt1 = ds_BCKD_QTTH.Tables["TONGHOP_BCKD"].Copy();
                        dgrHienThi.ItemsSource = dt1.DefaultView;
                    }
                }
                //=============     BIEU KDDN-10A     =======================
                if (optBCKD10_A.IsChecked == true)
                {
                    GlobalServices.Service_NPCUS.TH_BCTTTT(strMa_dviqly_InBC, i16ThangBC, i16NamBC, p_nhapthop,p_dncd);
                    MessageBox.Show("Tổng hợp biểu KDĐN-10A thành công");
                }
                //=============     BIEU KDDN-10B     =======================
                if (optBCKD10_B.IsChecked == true)
                {
                    GlobalServices.Service_NPCUS.TH_BCTTCC(strMa_dviqly_InBC, i16ThangBC, i16NamBC);
                    MessageBox.Show("Tổng hợp biểu KDĐN-10B thành công");
                }
                //=============     BIEU KDDN-10C     =======================
                if (optBCKD10_C.IsChecked == true)
                {
                    GlobalServices.Service_NPCUS.TH_BCTTDD(strMa_dviqly_InBC, i16ThangBC, i16NamBC,p_nhapthop);
                    MessageBox.Show("Tổng hợp biểu KDĐN-10C thành công");
                }


                //=============     BIEU KDDN-10D     =======================
                if (optBCKD10_D.IsChecked == true)
                {
                    MessageBox.Show("Biểu 10D không cần tổng hợp");
                    return;
                }
                if (optBCKD11.IsChecked == true)
                {
                    MessageBox.Show("Biểu 11 không cần tổng hợp");
                    return;
                }
                if (optBCKD12.IsChecked == true)
                {
                    MessageBox.Show("Biểu 12 không cần tổng hợp");
                    return;
                }
                if (optBCKD13.IsChecked == true)
                {                    
                    MessageBox.Show("Biểu 13 không cần tổng hợp");
                    return;
                }
                if (optBCKD4A.IsChecked == true)
                {
                    MessageBox.Show("Biểu 4A không cần tổng hợp");
                    return;
                }
                if (optBCKD4B.IsChecked == true)
                {
                    MessageBox.Show("Biểu 4B không cần tổng hợp");
                    return;
                }
                if (optBCKD5A.IsChecked == true)
                {
                    MessageBox.Show("Biểu 5A không cần tổng hợp");
                    return;
                }
                if (optBCKD5B.IsChecked == true)
                {
                    MessageBox.Show("Biểu 5B không cần tổng hợp");
                    return;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
            this.Cursor = null;
        }

        private void GrdBaoCaoKinhDoanh_Loaded(object sender, RoutedEventArgs e)
        {
            btnInBCKD.IsEnabled = true;
            btnTongHop.IsEnabled = false;
            btnLaymau.Visibility = Visibility.Hidden;
            btnGhimau.Visibility = Visibility.Hidden;
            if (intFormLoad == 1)
            {
                intFormLoad = intFormLoad + 1;
            }
        }

        private void GrdTongHopSoLieu_Loaded(object sender, RoutedEventArgs e)
        {

            if (intFormLoad == 2 || intFormLoad == 3)
            {
                btnInBCKD.IsEnabled = true;
                btnTongHop.IsEnabled = false;
                btnLaymau.Visibility = Visibility.Hidden;
                btnGhimau.Visibility = Visibility.Hidden;
            }
            else
            {
                btnInBCKD.IsEnabled = false;
                btnTongHop.IsEnabled = true;
                btnLaymau.Visibility = Visibility.Hidden;
                btnGhimau.Visibility = Visibility.Hidden;
                dtpThangNam_1.Text = i16ThangBC.ToString() + "/" + i16NamBC.ToString();
            }
            intFormLoad = intFormLoad + 1;
        }

        private void dtpThangNam_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                if (dtpThangNam.Text.ToString() != "")
                {
                    //if (dtpThangNam.Text.ToString().Length < 2)
                    //{
                    //    dtpThangNam.Text = "0" + dtpThangNam.Text.ToString();
                    //}
                    //if (Convert.ToInt16(dtpThangNam.Text.ToString()) > 60)
                    //{
                    //    dtpThangNam.Text = "00";
                    //}
                }
            }
        }

        private void optBCKD09_Click(object sender, RoutedEventArgs e)
        {

        }

        private void chk_mocdn2_Checked(object sender, RoutedEventArgs e)
        {
            try
            {
                    p_nhapthop = "2";
                    chk_moctpcd.IsEnabled = true;
            }
            catch (Exception ex)
            { MessageBox.Show(ex.ToString()); return; }
        }

        private void chk_mocdn2_Unchecked(object sender, RoutedEventArgs e)
        {
            try
            {
                    p_nhapthop = "TH";
                    chk_moctpcd.IsEnabled = false;
                    chk_moctpcd.IsChecked = false;

            }
            catch (Exception ex)
            { MessageBox.Show(ex.ToString()); return; }
        }

        private void chk_moctpcd_Checked(object sender, RoutedEventArgs e)
        {
            try
            {
                p_dncd = "Y";
            }
            catch (Exception ex)
            { MessageBox.Show(ex.ToString()); return; }
        }

        private void chk_moctpcd_Unchecked(object sender, RoutedEventArgs e)
        {
            try
            {
                p_dncd = "N";
            }
            catch (Exception ex)
            { MessageBox.Show(ex.ToString()); return; }
        }

        private void optBCKD10_A_Checked(object sender, RoutedEventArgs e)
        {
            try
            {
                chk_mocdn2.IsEnabled = true;
            }
            catch (Exception ex)
            { MessageBox.Show(ex.ToString()); return; }
        }

        private void optBCKD10_A_Unchecked(object sender, RoutedEventArgs e)
        {
            try
            {
                chk_mocdn2.IsEnabled = false;
                chk_moctpcd.IsEnabled = false;
                chk_moctpcd.IsChecked = false;
                chk_mocdn2.IsChecked = false;
            }
            catch (Exception ex)
            { MessageBox.Show(ex.ToString()); return; }

        }
        private void optBCKD10_C_Checked(object sender, RoutedEventArgs e)
        {
            try
            {
                chk_mocdn2.IsEnabled = true;
            }
            catch (Exception ex)
            { MessageBox.Show(ex.ToString()); return; }

        }

        private void optBCKD10_C_Unchecked(object sender, RoutedEventArgs e)
        {
            try
            {
                chk_mocdn2.IsEnabled = false;
                chk_moctpcd.IsEnabled = false;
                chk_moctpcd.IsChecked = false;
                chk_mocdn2.IsChecked = false;
            }
            catch (Exception ex)
            { MessageBox.Show(ex.ToString()); return; }
        }

        private void GrdTrangbia_Loaded(object sender, RoutedEventArgs e)
        {

        }

        private void txtNoiDung_GotFocus(object sender, RoutedEventArgs e)
        {

        }

        private void txtNoiDung_LostFocus(object sender, RoutedEventArgs e)
        {

        }

        private void txtNoiDung_KeyDown(object sender, KeyEventArgs e)
        {

        }

        private void Trangbia_GotFocus(object sender, RoutedEventArgs e)
        {
            btnInBCKD.IsEnabled = true;
            btnTongHop.IsEnabled = false;
            btnLaymau.Visibility = Visibility.Visible;
            btnGhimau.Visibility = Visibility.Visible;
            btnLaymau.IsEnabled = true;
        }

        private void btnLaymau_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                GET_THANGNAM_MADVIQLY_INBC();

                DataSet dsBia;
                dsBia = GlobalServices.Service_NPCUS.select_PC1_BIABC(strMa_dviqly_InBC);

                btnGhimau.IsEnabled = true;
                btnInBCKD.IsEnabled = true;

                if (dsBia != null)
                {
                    txtSobcao.Text = dsBia.Tables[0].Rows[0]["SO_BCAO"].ToString();
                    txtNoiDung.Text = dsBia.Tables[0].Rows[0]["NOI_DUNG"].ToString();
                    checkBiaBC = true;
                }
                else
                {
                    MessageBox.Show("Chưa có dữ liệu !");
                    checkBiaBC = false;
                    return;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }

        }

        private void btnGhimau_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                bool checkGhi = false;
                string strSobcao = txtSobcao.Text.Trim().ToString();
                string strNoidung = txtNoiDung.Text.Trim().ToString();

                GET_THANGNAM_MADVIQLY_INBC();

                if (checkBiaBC == false)
                {
                    checkGhi = GlobalServices.Service_NPCUS.insert_PC1_BIABC(strMa_dviqly_InBC, strNoidung, strSobcao);
                }
                else
                {
                    //cls_TienichNPC_bus cls = new cls_TienichNPC_bus();
                    checkGhi = GlobalServices.Service_NPCUS.update_PC1_BIABC(strMa_dviqly_InBC, strNoidung, strSobcao);
                        //GlobalServices.Service_NPCUS.update_PC1_BIABC(strMa_dviqly_InBC, strNoidung, strSobcao);
                }

                if (checkGhi == true)
                {
                    MessageBox.Show("Ghi dữ liệu thành công !");
                }
                else
                {
                    MessageBox.Show("Không ghi được !");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void Trangbia_LostFocus(object sender, RoutedEventArgs e)
        {
            
        }

        private void Trangbia_Loaded(object sender, RoutedEventArgs e)
        {
            //btnInBCKD.Visibility = Visibility.Visible;
            //btnTongHop.Visibility = Visibility.Hidden;
            btnInBCKD.IsEnabled = true;
            btnLaymau.Visibility = Visibility.Visible;
            btnGhimau.Visibility = Visibility.Visible;
        }

        private void chk_luykequy_Checked(object sender, RoutedEventArgs e)
        {
            try
            {
                p_quy = "Y";
            }
            catch (Exception ex)
            { MessageBox.Show(ex.ToString()); return; }
        }

        private void chk_luykequy_Unchecked(object sender, RoutedEventArgs e)
        {
            try
            {
                p_quy = "N";
            }
            catch (Exception ex)
            { MessageBox.Show(ex.ToString()); return; }
        }

        private void btnCheck_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                DataSet ds = new DataSet();
                cls_TienichNPC_bus cls = new cls_TienichNPC_bus();
                Int16 ky=0;
                Int16 thang = 0;
                Int16 nam = 0;
                string strMadviqly = "";
                string strMagocs = "";
                ky = 1;//Convert.ToInt16(txtky.Text);
                thang = 4;// Convert.ToInt16(txtthang.Text);
                nam = 2012; // Convert.ToInt16(txtnam.Text);
                strMadviqly = "PA13AS";// txtMadviqly.Text.ToString();
                strMagocs = "AS012"; // txtMasogcs.Text.ToString();

                //ds = GlobalServices.Service_NPCUS.togcs(nam, thang, ky, strMadviqly, strMagocs, "");
                //ds = cls.Togcs(nam, thang, ky, strMadviqly, strMagocs, "");

                //lblResult.Content = ds.Tables[0].Rows.Count.ToString();

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }


       

    }
        
}
